<template>
  <com-dialog ref="treeDialog" @confirm="confirm">
    <div slot="dialogBody">
      <el-tree
        ref="treeModal"
        v-if="refreshTable"
        :data="treeData"
        node-key="id"
        :default-expand-all="showAllNode"
        :default-expanded-keys="defaultShowNodes"
        :props="defaultProps"
        :current-node-key="currentLivingId"
        highlight-current
        @node-click="chooseNode"
      >
      </el-tree>
    </div>
  </com-dialog>
</template>

<script>
export default {
  data() {
    return {
      defaultProps: {
        id:'id',
        children: "children",
        label: "title",
      },
      treeData: [],
      treeModalShow: true,
      //树节点展示刷新
      refreshTable: true,
      //展开所有节点
      showAllNode: true,
      //默认展开节点
      defaultShowNodes: [],
      myNode: null,
      currentLivingId:null,
    };
  },

  methods: {
    setTreeData() {
      if (this.myNode) {
          this.currentLivingId = this.myNode.id;
        // this.$refs.treeModal.setCurrentNode(this.currentLivingId);
      }
    },
    confirm() {
        this.currentLivingId = this.myNode.id
      this.$emit("confirmUser", this.myNode);
    },
    showDialog() {
      this.$refs.treeDialog.showDialog();
      this.getDataList();
    },
    /**选择组织结构 */
    chooseNode(obj) {
      this.myNode = obj;
    },
    /**展示全部节点 */
    showAllTreeNode() {
      this.refreshTable = false;
      if (this.showAllNode) {
        this.showAllNode = false;
      } else {
        this.showAllNode = true;
      }
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /**刷新树节点数据 */
    loadTreeData() {
      this.getDataList();
      this.$message({
        message: "刷新树数据",
        type: "success",
      });
    },
    getDataList() {
      const _this = this;
      if (this.treeData.length) {
        _this.$nextTick(() => {
          _this.setTreeData();
        });
      } else {
        this.$request({
          url: { name: "deptTree", params: { isAll: true } },
          method: "GET",
        }).then((res) => {
          console.log(res);
          _this.treeData = res;
          _this.$nextTick(() => {
            _this.setTreeData();
            });
        });
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
